import { Component, Vue, Watch } from 'vue-property-decorator';
import { isEmpty } from "lodash";
/**
 * 这是一个查询头mixin类
 *
 * @Function onQueryClick 列表查询
 * @Function onQueryTreeClick 树形数据本地条件过滤
 * @Function onExportExcel 导出表格
 *
 * @export
 * @class mixinGridHeader
 * @extends {Vue}
 */
@Component({
})
export class mixinGridHeader extends Vue {
    // 数据变化动态监听列表
    watchList: any = {};
    headerSelectList = [];
    @Watch('headerSelectList', { immediate: true })
    updateStatusList(v: any) {
        this.updateEnquiryData(v);
    }
    updateEnquiryData(v: any) {
        if (!isEmpty(v)) {
            const me = this as any, searchHeader = me.$refs.searchHeader;
            if (searchHeader) {
                me.setEnquiryData(v, searchHeader);
            } else {
                me.$nextTick(() => {
                    me.setEnquiryData(v, me.$refs.searchHeader);
                })
            }
        }
    }

    setEnquiryData(v: any, searchHeader: any) {
        const me = this as any;
        v.forEach((item: any) => {
            const {
                dataName, keyName, funNmae
            } = item, data = me[dataName];
            if (!isEmpty(data)) {
                searchHeader.setSelectDataByField(keyName, data, item.isCascader)
            } else {
                me.watchList[dataName] = me.$watch(dataName, (v: any) => {
                    searchHeader.setSelectDataByField(keyName, v, item.isCascader)
                })
                if (funNmae) {
                    me[funNmae]();
                }
            }
        })
    }

    beforeDestroy() {
        const me = this as any;
        // 销毁动态监听
        for (const item in me.watchList) {
            me.watchList[item]()
        }
    }

    /**
     * 列表查询
     * 与zb-header-search扩展结合使用，用于查询按钮调用
     * 此方法必须引入mixinGridStore类才能使用
     * @param {*} params
     * @memberof mixinGridHeader
     */
    onQueryClick(params: any) {
        const me = this as any;
        me.proxyQuery(params);
    }
}
